﻿using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using HslCommunication.Core.Net;
using HslCommunication.LogNet;

namespace HslCommunication.Enthernet
{
	/// <summary>
	/// 一个基于明文的socket中心
	/// </summary>
	// Token: 0x020000F9 RID: 249
	public class NetPlainSocket : NetworkXBase
	{
		/// <summary>
		/// 实例化一个默认的对象
		/// </summary>
		// Token: 0x06001508 RID: 5384 RVA: 0x0006D334 File Offset: 0x0006B534
		public NetPlainSocket()
		{
			this.buffer = new byte[this.bufferLength];
			this.encoding = Encoding.UTF8;
		}

		/// <summary>
		/// 使用指定的ip地址和端口号来实例化这个对象
		/// </summary>
		/// <param name="ipAddress">Ip地址</param>
		/// <param name="port">端口号</param>
		// Token: 0x06001509 RID: 5385 RVA: 0x0006D398 File Offset: 0x0006B598
		public NetPlainSocket(string ipAddress, int port)
		{
			this.buffer = new byte[this.bufferLength];
			this.encoding = Encoding.UTF8;
			this.ipAddress = ipAddress;
			this.port = port;
		}

		/// <summary>
		/// 连接服务器
		/// </summary>
		/// <returns>返回是否连接成功</returns>
		// Token: 0x0600150A RID: 5386 RVA: 0x0006D40C File Offset: 0x0006B60C
		public OperateResult ConnectServer()
		{
			Socket coreSocket = this.CoreSocket;
			if (coreSocket != null)
			{
				coreSocket.Close();
			}
			OperateResult<Socket> operateResult = base.CreateSocketAndConnect(this.ipAddress, this.port, 5000);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				try
				{
					this.CoreSocket = operateResult.Content;
					this.CoreSocket.BeginReceive(this.buffer, 0, this.bufferLength, SocketFlags.None, new AsyncCallback(this.ReceiveCallBack), this.CoreSocket);
					result = OperateResult.CreateSuccessResult();
				}
				catch (Exception ex)
				{
					result = new OperateResult(ex.Message);
				}
			}
			return result;
		}

		/// <summary>
		/// 关闭当前的连接对象
		/// </summary>
		/// <returns>错误信息</returns>
		// Token: 0x0600150B RID: 5387 RVA: 0x0006D4B8 File Offset: 0x0006B6B8
		public OperateResult ConnectClose()
		{
			OperateResult result;
			try
			{
				Socket coreSocket = this.CoreSocket;
				if (coreSocket != null)
				{
					coreSocket.Close();
				}
				result = OperateResult.CreateSuccessResult();
			}
			catch (Exception ex)
			{
				result = new OperateResult(ex.Message);
			}
			return result;
		}

		/// <summary>
		/// 发送字符串到网络上去
		/// </summary>
		/// <param name="text">文本信息</param>
		/// <returns>发送是否成功</returns>
		// Token: 0x0600150C RID: 5388 RVA: 0x0006D504 File Offset: 0x0006B704
		public OperateResult SendString(string text)
		{
			bool flag = string.IsNullOrEmpty(text);
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateSuccessResult();
			}
			else
			{
				result = base.Send(this.CoreSocket, this.encoding.GetBytes(text));
			}
			return result;
		}

		// Token: 0x0600150D RID: 5389 RVA: 0x0006D540 File Offset: 0x0006B740
		private void ReceiveCallBack(IAsyncResult ar)
		{
			Socket socket = ar.AsyncState as Socket;
			bool flag = socket != null;
			if (flag)
			{
				byte[] array = null;
				try
				{
					int num = socket.EndReceive(ar);
					socket.BeginReceive(this.buffer, 0, this.bufferLength, SocketFlags.None, new AsyncCallback(this.ReceiveCallBack), socket);
					bool flag2 = num == 0;
					if (flag2)
					{
						Socket coreSocket = this.CoreSocket;
						if (coreSocket != null)
						{
							coreSocket.Close();
						}
						return;
					}
					array = new byte[num];
					Array.Copy(this.buffer, 0, array, 0, num);
				}
				catch (ObjectDisposedException)
				{
				}
				catch (Exception ex)
				{
					ILogNet logNet = base.LogNet;
					if (logNet != null)
					{
						logNet.WriteWarn(StringResources.Language.SocketContentReceiveException + ":" + ex.Message);
					}
					ThreadPool.QueueUserWorkItem(new WaitCallback(this.ReConnectServer), null);
				}
				bool flag3 = array != null;
				if (flag3)
				{
					Action<string> receivedString = this.ReceivedString;
					if (receivedString != null)
					{
						receivedString(this.encoding.GetString(array));
					}
				}
			}
		}

		/// <summary>
		/// 是否是处于重连的状态
		/// </summary>
		/// <param name="obj">无用的对象</param>
		// Token: 0x0600150E RID: 5390 RVA: 0x0006D660 File Offset: 0x0006B860
		private void ReConnectServer(object obj)
		{
			ILogNet logNet = base.LogNet;
			if (logNet != null)
			{
				logNet.WriteWarn(StringResources.Language.ReConnectServerAfterTenSeconds);
			}
			for (int i = 0; i < 10; i++)
			{
				Thread.Sleep(1000);
				ILogNet logNet2 = base.LogNet;
				if (logNet2 != null)
				{
					logNet2.WriteWarn(string.Format("Wait for connecting server after {0} seconds", 9 - i));
				}
			}
			OperateResult<Socket> operateResult = base.CreateSocketAndConnect(this.ipAddress, this.port, 5000);
			bool flag = !operateResult.IsSuccess;
			if (flag)
			{
				ThreadPool.QueueUserWorkItem(new WaitCallback(this.ReConnectServer), obj);
			}
			else
			{
				object obj2 = this.connectLock;
				lock (obj2)
				{
					try
					{
						Socket coreSocket = this.CoreSocket;
						if (coreSocket != null)
						{
							coreSocket.Close();
						}
						this.CoreSocket = operateResult.Content;
						this.CoreSocket.BeginReceive(this.buffer, 0, this.bufferLength, SocketFlags.None, new AsyncCallback(this.ReceiveCallBack), this.CoreSocket);
						ILogNet logNet3 = base.LogNet;
						if (logNet3 != null)
						{
							logNet3.WriteWarn(StringResources.Language.ReConnectServerSuccess);
						}
					}
					catch (Exception ex)
					{
						ILogNet logNet4 = base.LogNet;
						if (logNet4 != null)
						{
							logNet4.WriteWarn(StringResources.Language.RemoteClosedConnection + ":" + ex.Message);
						}
						ThreadPool.QueueUserWorkItem(new WaitCallback(this.ReConnectServer), obj);
					}
				}
			}
		}

		/// <summary>
		/// 当接收到字符串时候的触发事件
		/// </summary>
		// Token: 0x1400002E RID: 46
		// (add) Token: 0x0600150F RID: 5391 RVA: 0x0006D800 File Offset: 0x0006BA00
		// (remove) Token: 0x06001510 RID: 5392 RVA: 0x0006D838 File Offset: 0x0006BA38
		[DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public event Action<string> ReceivedString;

		/// <summary>
		/// 当前的编码器
		/// </summary>
		// Token: 0x170004F9 RID: 1273
		// (get) Token: 0x06001511 RID: 5393 RVA: 0x0006D86D File Offset: 0x0006BA6D
		// (set) Token: 0x06001512 RID: 5394 RVA: 0x0006D875 File Offset: 0x0006BA75
		public Encoding Encoding
		{
			get
			{
				return this.encoding;
			}
			set
			{
				this.encoding = value;
			}
		}

		/// <summary>
		/// 返回表示当前对象的字符串
		/// </summary>
		/// <returns>字符串</returns>
		// Token: 0x06001513 RID: 5395 RVA: 0x0006D87E File Offset: 0x0006BA7E
		public override string ToString()
		{
			return string.Format("NetPlainSocket[{0}:{1}]", this.ipAddress, this.port);
		}

		// Token: 0x040004FF RID: 1279
		private Encoding encoding;

		// Token: 0x04000500 RID: 1280
		private object connectLock = new object();

		// Token: 0x04000501 RID: 1281
		private string ipAddress = "127.0.0.1";

		// Token: 0x04000502 RID: 1282
		private int port = 10000;

		// Token: 0x04000503 RID: 1283
		private int bufferLength = 2048;

		// Token: 0x04000504 RID: 1284
		private byte[] buffer = null;
	}
}
